package stack;

import java.util.ArrayDeque;
import java.util.Deque;

public class dailyTemperatures {
    public int[] dailyTemperatures(int[] temperatures) {
        Deque<Integer> stack = new ArrayDeque();
        Deque<Integer> help = new ArrayDeque();
        int[] arr = new int[temperatures.length];
        for (int i = 0; i < temperatures.length; i++) {
            // 存放value和index
            if (stack.isEmpty()) {
                stack.push(temperatures[i]);
                help.push(i);
            } else {
                if (temperatures[i] <= stack.peek()) {
                    stack.push(temperatures[i]);
                    help.push(i);
                }else{
                    while (stack.peek()!=null&&temperatures[i] > stack.peek()) {
                        //可能不止一个
                        //先出后入
                        Integer value = stack.pop();
                        Integer index = help.pop();
                        arr[index] = i-index;
                    }
                    stack.push(temperatures[i]);
                    help.push(i);
                }
            }
        }
        return arr;
    }
    public static void main(String[] args) {
        // int[] arr = new int[]{73,74,75,71,69,72,76,73};
        // int[] arr = new int[]{30,40,50,60};
        int[] arr = new int[]{30,60,90};
        dailyTemperatures obj = new dailyTemperatures();
       int[] dailyTemperatures = obj.dailyTemperatures(arr);
       for(int day:dailyTemperatures){
        System.out.println(day);
       }
    }
}
